<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      width="63%"
      :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"
               :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="所属站点" prop="siteId"
                          :rules="[
                            {required: true, message:'站点不能为空', trigger:'blur'}
                 ]">
              <el-select v-model="inputForm.siteId" placeholder="请选择站点">
                <el-option
                  v-for="item in siteList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模型名称" prop="name"
                          :rules="[
                  {required: true, message:'模型名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写模型名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="英文名称" prop="enname"
                          :rules="[
                  {required: true, message:'英文名称不能为空', trigger:'blur'},
                  {validator: validator.isEnglish, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.enname" placeholder="请填写英文名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据模型" prop="dataModel"
                          :rules="[
                            {required: true, message:'数据模型不能为空', trigger:'blur'},
                  {validator: validator.isEnglish, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.dataModel" placeholder="请填写数据模型"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="前端列表视图" prop="frontList"
                          :rules="[
                            {required: true, message:'前端列表视图不能为空', trigger:'blur'},
                  {validator: validator.isEnglish, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.frontList" placeholder="请填写前端列表视图"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="前端详情视图" prop="frontForm"
                          :rules="[
                            {required: true, message:'前端详情视图不能为空', trigger:'blur'},
                  {validator: validator.isEnglish, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.frontForm" placeholder="请填写前端详情视图"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分页显示" prop="pageFlag"
                          :rules="[
                 ]">
              <el-switch
                v-model="inputForm.pageFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页显示" prop="homeFlag"
                          :rules="[
                 ]">
              <el-switch
                v-model="inputForm.homeFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页推荐" prop="positionFlag"
                          :rules="[
                 ]">
              <el-switch
                v-model="inputForm.positionFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="启用" prop="enableFlag"
                          :rules="[
                 ]">
              <el-switch
                v-model="inputForm.enableFlag"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                          :rules="[
                  {required: true, message:'排序不能为空', trigger:'blur'},
                  {validator: validator.isNumber, trigger:'blur'}
                 ]">
              <el-input-number size="small" v-model="inputForm.sort" :min="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                          :rules="[
                 ]">
              <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
import SelectTree from '@/components/treeSelect/treeSelect.vue'

export default {
  props: {
    siteList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        name: '',
        enname: '',
        dataModel: '',
        frontList: '',
        frontForm: '',
        pageFlag: '',
        homeFlag: '',
        positionFlag: '',
        enableFlag: 1,
        remarks: '',
        sort: '',
        siteId: ''
      }
    }
  },
  components: {
    SelectTree
  },
  methods: {
    init (method, obj) {
      this.method = method
      this.inputForm.id = obj.id
      if (method === 'add') {
        this.title = `新建模型`
      } else if (method === 'edit') {
        this.title = '修改模型'
      } else if (method === 'view') {
        this.title = '查看模型'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/cms/model/cmsModel/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.cmsModel)
            this.loading = false
          })
        } else {
          this.inputForm.siteId = obj.siteId
        }
      })
    },
    // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/cms/model/cmsModel/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    }
  }
}
</script>
